package test;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.finance.entity.Sign;
import com.finance.entity.customer.CallRecordInfo;
import com.finance.entity.customer.JingdongConsumption;
import com.finance.entity.customer.TaobaoConsumption;
import com.finance.service.SignService;
import com.finance.service.customer.CallRecordInfoService;
import com.finance.service.customer.JingdongConsumptionService;
import com.finance.service.customer.TaobaoConsumptionService;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class SignText {

	@Resource(name = "TaobaoConsumptionService")
	private TaobaoConsumptionService taobaoConsumptionService;

	@Resource(name = "SignService")
	private SignService signService;
	
	@Resource(name = "JingdongConsumptionService")
	private JingdongConsumptionService jingdongConsumptionService;
	
	@Resource(name = "CallRecordInfoService")
	private CallRecordInfoService callRecordInfoService;

	@Test
	public void test() throws ParseException {
		Sign sign = signService.selectByPrimaryKey(1);
		JSONObject json = JSONObject.fromObject(sign.getJson());
		JSONObject aJsonObject = json.getJSONObject("data");
		JSONArray jsonarr= aJsonObject.getJSONArray("orderDetails");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for (int i = 0; i < jsonarr.size(); i++) {
			JSONObject or = (JSONObject) jsonarr.get(i);
			TaobaoConsumption taobaoConsumption = new TaobaoConsumption();
			taobaoConsumption.setKfUserId("1111111");
			taobaoConsumption.setOrderId(or.getString("orderId"));
			taobaoConsumption
					.setOrderTime(sdf.parse(or.getString("orderTime")));
			taobaoConsumption.setOrderStatus(or.getString("orderStatus"));
			JSONArray items= or.getJSONArray("items");
			String s1 = items.toString();
			String s2 = s1.substring(0,s1.length()-1);
			String s3 = s2.substring(1, s2.length());
			JSONObject jsonObj = JSONObject.fromObject(s3);
			taobaoConsumption.setOrderAmt(jsonObj.getLong("itemPrice"));
			taobaoConsumption.setItems(jsonObj.getString("itemName"));
			
			taobaoConsumption.setItemQuantity(jsonObj.getInt("itemQuantity"));
			taobaoConsumption.setReceivePersonName(or.getJSONObject("logisticsInfo").getString("receivePersonName"));
			taobaoConsumption.setReceivePersonMobile(or.getJSONObject("logisticsInfo").getString("receivePersonMobile"));
			taobaoConsumption.setReceiveAddress(or.getJSONObject("logisticsInfo").getString("receiveAddress"));
			taobaoConsumptionService.insert(taobaoConsumption);
			System.err.println("淘宝信息添加成功" + "次数：" + i);
		}
	}
	
	@Test
	public void test1() throws ParseException {
		Sign sign = signService.selectByPrimaryKey(2);
		JSONObject json = JSONObject.fromObject(sign.getJson());
		JSONObject aJsonObject = json.getJSONObject("data");
		JSONArray jsonarr= aJsonObject.getJSONArray("orderDetail");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for (int i = 0; i < jsonarr.size(); i++) {
			JSONObject or = (JSONObject) jsonarr.get(i);
			JingdongConsumption jd = new JingdongConsumption();
			jd.setGoodsName(or.getString("goodsName"));
			jd.setConsigneeAddr(or.getString("consigneeAddr"));
			jd.setOrderDate(sdf.parse(or.getString("orderDate")));
			jd.setOrderMoney(or.getLong("orderMoney"));
			jd.setConsigneePerson(or.getString("consigneePerson"));
			jd.setTel(or.getString("tel"));
			jd.setOrderStatus(or.getString("orderStatus"));
			jd.setPayType("payType");
			jd.setKfUserId("1111111");
			jingdongConsumptionService.insert(jd);
			System.err.println("京东信息添加成功" + "次数：" + i);
		}
	}
	
	@Test
	public void te() throws ParseException{
		Sign sign = signService.selectByPrimaryKey(3);
		JSONObject json = JSONObject.fromObject(sign.getJson());
		JSONObject aJsonObject = json.getJSONObject("data");
		JSONArray jsonarr = aJsonObject.getJSONArray("callRecordInfo");
		SimpleDateFormat sdf1 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		for (int i = 0; i < jsonarr.size(); i++) {
			JSONObject ca = (JSONObject) jsonarr.get(i);
			CallRecordInfo caInfo = new CallRecordInfo();
			caInfo.setKfUserId("1111111");
			caInfo.setCallAddress(ca.getString("callAddress"));
			caInfo.setCallDateTime(sdf1.parse(ca
					.getString("callDateTime")));
			caInfo.setCallTimeLength(ca.getString("callTimeLength"));
			String typeString = ca.getString("callType");
			if (typeString.equals("主叫")) {
				caInfo.setCallType(1);
			} else {
				caInfo.setCallType(2);
			}
			caInfo.setMobileNo(ca.getString("mobileNo"));
			callRecordInfoService.insert(caInfo);
			System.err.println("运营商信息添加成功" + "次数：" + i);
		}
	}
	
	@Test
	public void pdd(){
		Sign sign = signService.selectByPrimaryKey(4);
		JSONObject json = JSONObject.fromObject(sign.getJson());
		JSONArray jsonarr = json.getJSONArray("contacts");
		for (int i = 0; i < jsonarr.size(); i++) {
			JSONObject c = (JSONObject) jsonarr.get(i);
			String nameString = c.getString("name");
			System.err.println(nameString);
		}
		System.out.println("1");
	}
	
	
}
